#include "main.h"

#ifdef DEBUG
void usage(char *str){
    printf("USAGE: %s in-file.wav [-]\n", str);
}
#endif

int main(int argc, char** argv) {
    WaveData embededVoice = {0, 0, NULL};
    int result;
    WAVHeader header;
    unsigned long i;
    unsigned long extracted;
    short *where, *initialWhere;

    /* Loading the voice from the file */
    if (argc > 2 && *argv[2] == '-') {
        result = loadSamples(&embededVoice, argv[1]);
    } else if (argc > 1) {
        result = loadVoice(&embededVoice, &header, argv[1]);
    } else {
#ifdef DEBUG
        usage(argv[0]);
        return 1;
#endif
    }

    /* Calling the extracting method */
    if (result) {
        DWT_INIT();
#ifdef DEBUG
        printf("size is: %d\n", embededVoice.soundLength);
#endif
        initialWhere = (short*) embededVoice.sample;
        for (i = 0; i < embededVoice.soundLength; i++) {

            where = (short*) &embededVoice.sample[i];
            if ((i) % 1 == 0 && (argc > 2 && *argv[2] == 'v' || argc > 3 && *argv[3] == 'v')) {
#ifdef DEBUG
                printf("A new value: %02d   %02x\n", *(unsigned char *) where, *(unsigned char*) where);
#endif
            }

            /* Searching the syn mark... */
            if (testSynMark(where) == 0) {
                /* if found! */
                extracted = extractMethod(where + SYN_NSAMPLES);
#ifdef DEBUG
                printf("MARK FOUND! -> extracted: %ld. position=%ld\n", extracted, i / 2);
#endif
            }
        }
    }

    /* free allocated dynamic memory */
    if (embededVoice.sample)
        free(embededVoice.sample);

#ifdef DEBUG
    printf("PROCESS ENDED OK!!!\n");
#endif
    return 0;
}

